<template>
  <div>
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="ID" > </el-table-column>
      <el-table-column prop="title" label="标题"> </el-table-column>
      <el-table-column label="类型">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.type === 1">目录</el-tag>
          <el-tag type="danger" v-if="scope.row.type === 2">菜单</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="图标" >
        <template slot-scope="scope">
          <i :class="scope.row.icon"></i>
        </template>
      </el-table-column>
      <el-table-column prop="url" label="地址" > </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status === 1">已启用</el-tag>
          <el-tag type="danger" v-if="scope.row.status === 2">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" >
        <template slot-scope="scope">
          <el-button
            type="primary"
            @click="edit(scope.row)"
            icon="el-icon-edit"
            circle
            size="mini"
          ></el-button>
          <el-button
            type="danger"
            @click="del(scope.row.id)"
            size="mini"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableData: [],
    };
  },
  created() {
    this.getTableData();
  },
  methods: {
    getTableData() {
      this.$http
        .get("/menulist?istree=1")
        .then((res) => {
          this.tableData = res.list;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    edit(data) {
      this.$emit('edit',data)
    },
    del(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //   this.$message({
          //     type: 'success',
          //     message: '删除成功!'
          //   });
          this.delItem(id);
        })
        .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
        });
    },
    delItem(id) {
      this.$http
        .post("/menudelete", { id: id })
        .then((res) => {
          if (res.code === 200) {
            this.$message({
              type: "success",
              message: res.msg,
            });
            this.tableData = res.list;
          } else {
            this.$message({
              type: "info",
              message: res.msg,
            });
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>
<style>
</style>